package com.renshine.swing.component.panel;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import org.springframework.stereotype.Component;

import com.renshine.base.bean.User;
import com.renshine.base.util.FileUtil;

@Component
@SuppressWarnings("serial")
public class LoginPanel extends JPanel {

	private JComboBox<String> dialogBox; // 选择方言
	private JTextField jdbcUrl; // 数据库连接址
	private JTextField username; // 用户名
	private JTextField password; // 密码

	public LoginPanel() {
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		{
			JLabel dialogLab = new JLabel("数据库");
			dialogLab.setHorizontalAlignment(SwingConstants.RIGHT);
			dialogLab.setBounds(50, 40, 54, 15);
			add(dialogLab);
		}
		{
			JLabel jdbcUrlLab = new JLabel("连接地址");
			jdbcUrlLab.setHorizontalAlignment(SwingConstants.RIGHT);
			jdbcUrlLab.setBounds(50, 65, 54, 15);
			add(jdbcUrlLab);
		}
		{
			JLabel usernameLab = new JLabel("用户名");
			usernameLab.setHorizontalAlignment(SwingConstants.RIGHT);
			usernameLab.setBounds(50, 90, 54, 15);
			add(usernameLab);
		}
		{
			JLabel passwordLab = new JLabel("密码");
			passwordLab.setHorizontalAlignment(SwingConstants.RIGHT);
			passwordLab.setBounds(50, 118, 54, 15);
			add(passwordLab);
		}
		{
			jdbcUrl = new JTextField();
			jdbcUrl.setHorizontalAlignment(SwingConstants.LEFT);
			jdbcUrl.setBounds(114, 62, 225, 21);
			add(jdbcUrl);
			jdbcUrl.setColumns(15);
		}
		{
			username = new JTextField();
			username.setHorizontalAlignment(SwingConstants.LEFT);
			username.setBounds(114, 87, 225, 21);
			add(username);
			username.setColumns(10);
		}
		{
			password = new JTextField();
			password.setHorizontalAlignment(SwingConstants.LEFT);
			password.setBounds(114, 115, 225, 21);
			add(password);
			password.setColumns(10);
		}
		{
			dialogBox = new JComboBox<String>(FileUtil.getDatabases());
			//dialogBox.setSelectedIndex(-1);
			dialogBox.setBounds(114, 37, 144, 21);
			add(dialogBox);
		}
	}

	public JComboBox<String> getDialogBox() {
		return dialogBox;
	}

	public void setDialogBox(JComboBox<String> dialogBox) {
		this.dialogBox = dialogBox;
	}

	public JTextField getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(JTextField jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	public JTextField getUsername() {
		return username;
	}

	public void setUsername(JTextField username) {
		this.username = username;
	}

	public JTextField getPassword() {
		return password;
	}

	public void setPassword(JTextField password) {
		this.password = password;
	}

	/**
	 * 初始化面版
	 * @param user
	 */
	public void init(User user) {
		if (user != null) {			
			this.dialogBox.setSelectedItem(user.getDialect());
			this.username.setText(user.getUsername());
			this.password.setText(user.getPassword());
			this.jdbcUrl.setText(user.getJdbcUrl());
		}
	}
	
	/**
	 * 取得登录的信息
	 * 
	 * @return
	 */
	public User getUser() {
		return new User(dialogBox.getSelectedItem().toString(), jdbcUrl.getText(), username.getText(), password.getText());
	}
}
